#!/bin/bash -e
# Setup the repository and local system for development

cd "$(dirname "$0")/.."

HELPER=./contrib/ci/fwupd_setup_helpers.py
HELPER_ARGS="-y"

rename_branch()
{
    OLD=master
    NEW=main
    if git log $OLD >/dev/null 2>&1 &&
       git remote get-url origin 2>&1 | grep fwupd/fwupd.git >/dev/null 2>&1; then
        read -p "Rename existing $OLD branch to $NEW? (y/N) " question
        if [ "$question" = "y" ]; then
            git branch -m $OLD $NEW
            git fetch origin
            git branch -u origin/$NEW $NEW
            git remote set-head origin -a
        fi
    fi
}

detect_selinux()
{
    SELINUX="0"
    if which getenforce &> /dev/null; then
        if getenforce | grep -i "enforcing" >/dev/null; then
            SELINUX="1"
        fi
    fi
}

setup_deps()
{
    read -p "Install build dependencies? (y/N) " question
    if [ "$question" = "y" ]; then
        $(which sudo) python3 $HELPER install-dependencies $HELPER_ARGS -y
    fi
}

setup_run_dev()
{
    if [ "$SELINUX" -eq "1" ]; then
        echo "SELinux is enabled, you won't be able to run the daemon from default prefix /usr/local"
        return
    fi
    read -p "Set up dbus activated daemon and PolicyKit actions from /usr/local? (y/N) " question
    if [ "$question" = "y" ]; then
        ./contrib/prepare-system /usr/local install
    fi
}

setup_unsafe_polkit_rules()
{
    read -p "Install developer-friendly **unsafe** PolicyKit rules into /etc/polkit-1/rules.d? (y/N) " question
    if [ "$question" = "y" ]; then
        sudo mkdir -p /etc/polkit-1/rules.d
        sudo cp ./policy/org.freedesktop.fwupd-unsafe.rules /etc/polkit-1/rules.d/
    fi
}

setup_vscode()
{
    # Add default vscode settings if not existing
    SETTINGS_FILE=./.vscode/settings.json
    SETTINGS_TEMPLATE_FILE=./contrib/vscode/settings.json
    if [ ! -f "$SETTINGS_FILE" ]; then
        mkdir ./.vscode
        echo "Copy $SETTINGS_TEMPLATE_FILE to $SETTINGS_FILE."
        cp "$SETTINGS_TEMPLATE_FILE" "$SETTINGS_FILE"
    fi
}

setup_git()
{
    echo "Configuring git environment"
    git config include.path ../.gitconfig
}

install_pip()
{
    package=$1
    args=$2
    if ! python3 -m pip install $package $args; then
        $(which sudo) python3 $HELPER install-pip $HELPER_ARGS -y
    fi
    #try once more
    python3 -m pip install $package
}

setup_precommit()
{
    echo "Configuring pre-commit hooks"
    python3 -m venv venv
    source venv/bin/activate

    install_pip pre-commit
    pre-commit install
}

setup_prepush()
{
    read -p "Run tests locally before pushing to remote branches? THIS WILL SLOW DOWN EVERY PUSH but reduce the risk of failing CI. (y/N) " question
    if [ "$question" = "y" ]; then
        pre-commit install -t pre-push
    else
        pre-commit uninstall -t pre-push
    fi
}

check_markdown()
{
    python3 $HELPER test-markdown
}

check_jinja2()
{
    python3 $HELPER test-jinja2
}

check_meson()
{
    python3 $HELPER test-meson
}

detect_os()
{
    for i in "$@"; do
        case $i in
            --os=*)
                OS="${i#*=}"
                shift
                ;;
            --debug)
                DEBUG=1
                shift
                ;;
            *)
                ;;
        esac
    done
    if [ -z $OS ]; then
        OS=$(python3 $HELPER detect-profile)
        if [ -z "$OS" ]; then
            install_pip distro
            OS=$(python3 $HELPER detect-profile)
        fi
        echo "Using OS profile $OS to setup"
    fi
    if [ -n "$OS" ];then
        HELPER_ARGS="$HELPER_ARGS --os $OS"
    fi
    if [ -n "$DEBUG" ]; then
        set -x
        HELPER_ARGS="$HELPER_ARGS --debug"
    fi
}

#needed for arguments for some commands
detect_os "$@"

detect_selinux

#if interactive install build deps and prepare environment
if [ -t 2 ]; then
    case $OS in
        debian|ubuntu|arch|fedora)
            setup_deps
            setup_run_dev
            ;;
        void)
            setup_deps
            ;;
    esac
    setup_unsafe_polkit_rules
    rename_branch
fi
check_markdown
check_jinja2
setup_vscode
setup_git
check_meson
setup_precommit

#needs to be after pre-commit is sourced
if [ -t 2 ]; then
    setup_prepush
fi
